package com.example.demo.enums;

import com.example.demo.model.msgFromCQ.ReceiveMsg;

import java.util.HashMap;
import java.util.Map;

public enum  RobotAppidEnum {

    DEFAULT(null, -1, "cebaf94c551f180d5c6847cf1ccaa1fa");

    private String type;
    private long id;
    private String appid;

    private static Map<Long, String> privateAppidMap = new HashMap<>();
    private static Map<Long, String> groupAppidMap = new HashMap<>();

    RobotAppidEnum(String type, long id, String appid) {
        this.type = type;
        this.id = id;
        this.appid = appid;
    }

    public static String getAppidByMsg(ReceiveMsg receiveMsg){
        String message_type = receiveMsg.getMessage_type();
        if("private".equals(message_type)){
            return getAppid(message_type, receiveMsg.getUser_id());
        }
        if("group".equals(message_type)){
            return getAppid(message_type, receiveMsg.getGroup_id());
        }
        return RobotAppidEnum.DEFAULT.appid;
    }

    public static String getAppid(String type, long id){
        Map<Long, String> appidMap = getAppidMap(type);
        String appid = appidMap.get(id);
        return appid == null ? RobotAppidEnum.DEFAULT.appid : appid;
    }

    public static Map<Long, String> getAppidMap(String type){
        if("private".equals(type)){
            if(privateAppidMap.size()==0){
                initPrivateAppidMap();
            }
            return privateAppidMap;
        }
        if("group".equals(type)){
            if(privateAppidMap.size()==0){
                initGroupAppidMap();
            }
            return groupAppidMap;
        }
        return null;
    }

    private static void initPrivateAppidMap(){
        RobotAppidEnum[] values = RobotAppidEnum.values();
        for(RobotAppidEnum item : values){
            if("private".equals(item.type)){
                privateAppidMap.put(item.id, item.appid);
            }
        }
    }

    private static void initGroupAppidMap(){
        RobotAppidEnum[] values = RobotAppidEnum.values();
        for(RobotAppidEnum item : values){
            if("group".equals(item.type)){
                groupAppidMap.put(item.id, item.appid);
            }
        }
    }
}
